//
//  HCNewReAssignSearchResultViewController.m
//  HCNewGoldFinger
//
//  Created by zjhcsoft on 14-10-15.
//  Copyright (c) 2014年 zjhcsoftios. All rights reserved.
//

#import "HCNewReAssignSearchResultViewController.h"
#import "ZSBContactInfo.h"
#import "ZSBContactInfoCell.h"
#import "ZSBWorkOrderViewController.h"
#import "SendWorkOrderAndUploadImageController.h"
#import "UIColor+EX.h"
#import "iSeeNetworkRequest.h"
#import "NetworkAddress.h"
#import "SVProgressHUD.h"

@interface HCNewReAssignSearchResultViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSNumber * userRowId;

@property (nonatomic, strong) NSString *redirectBz;

@end

@implementation HCNewReAssignSearchResultViewController


- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"EFEFF4"];
    
    UIButton *leftBtnView = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtnView.tag = 1;
    leftBtnView.frame = CGRectMake(0, 0, 28, 28);
    [leftBtnView setImage:[UIImage imageNamed:@"toolbar_btn_back"] forState:UIControlStateNormal];
    [leftBtnView addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:leftBtnView];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
    if (_titleName) {
        self.title = _titleName;
    }
    

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.autoresizingMask = ~UIViewAutoresizingNone;
    _tableView.backgroundView = nil;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)back:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)goBackToListView
{
    
    NSArray * array = self.navigationController.viewControllers;
    for (NSInteger i = array.count-1; i >= 0; i--)
    {
        UIViewController *ctrl = array[i];
        if ([ctrl isKindOfClass:NSClassFromString(@"ZSBWorkOrderViewController")] || [ctrl isKindOfClass:NSClassFromString(@"ZSBWorkOrderListViewController")] || [ctrl isKindOfClass:NSClassFromString(@"NewMissonListController")]) {

            [self.navigationController popToViewController:ctrl animated:YES];
            break;
        }
    }
    
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dateList.count;
}
#pragma mark -
#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZSBContactInfo *contactInfo = [_dateList objectAtIndex:indexPath.row];
    if (contactInfo.contactType == ZSBContactTypeCompany) {
        return 44;
    }else{
        return 60;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellName = @"reAssignSearchCell";
    ZSBContactInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[ZSBContactInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    ZSBContactInfo *contactInfo = [_dateList objectAtIndex:indexPath.row];
    cell.showCompany = YES;
    cell.contactInfo = contactInfo;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZSBContactInfo *contactInfo = [_dateList objectAtIndex:indexPath.row];
    
    self.userRowId = contactInfo.sysUserRowId;

    //派单或者转派
    if (_isPD) {
        
        [self paidan];
        
        
        
    }else {
        
        SendWorkOrderAndUploadImageController *abc = [[SendWorkOrderAndUploadImageController alloc] init];
        abc.tpye = 1;
        abc.billIds = self.billIds;
        abc.userRowId = self.userRowId;
        abc.zpRemarkFlag = self.zpRemarkFlag;
        abc.zpHdImageFlag = self.zpHdImageFlag;
        abc.isReceiveAndTransfer = self.isReceiveAndTransfer;
        [self.navigationController pushViewController:abc animated:YES];
        
    }
}


#pragma mark 派单

- (void)paidan {
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:self.billIds forKey:@"orderIds"];
    [parmas setObject:self.userRowId forKey:@"pgUserId"];
    [SVProgressHUD setStatus:@"派单中"];
    
    [iSeeNetworkRequest getWithHeaderUrl:kFormat(@"%@%@",NMarketMainUrl,ZPpaidan) params:parmas success:^(id object) {
        [self performSelector:@selector(goBackToListView) withObject:nil afterDelay:0];
        [SVProgressHUD showSuccessWithStatus:@"派单成功"];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"派单失败"];
    }];
    
    
}

@end
